import { Component} from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
@Component({
  selector: 'app-musicedit',
  templateUrl: './musicedit.component.html',
  styleUrls: ['./musicedit.component.scss']
})
export class MusiceditComponent {
  /**
   * @param {any} musicData  
   * 接收 getMusicById() 方法 传递过来的参数
   * @param {string}  user 
   * 接收 user表里的参数
   * @param {string} album  
   * 接收 album表里的参数
   */
  musicData:any={
    user:'',
    album:""
  }
  /**
   * @param {boolean} isUpdate 
   * 更新状态的情况
   */
  isUpdate:boolean = true;
  /**
   * @param route 导入激活路由对象
   * @param router 导入路由Router
   */
  //ActivatedRoute当前激活路由对象，主要用于保存路由，获取路由传递的参数
  constructor(private route:ActivatedRoute,private router:Router){
    // 获取当前要编辑的歌单的ID
    this.route.queryParamMap.subscribe(data=>{
      let params:any = data
      params = params.params
      if(params?.id){
        this.getMusicById(params.id)
      }
      if(params?.new){
        this.isUpdate = false;
      }
    })
  }

  /**
   * @property {function} back 
   * 跳转到歌单页面
   */
  back(){
    this.router.navigate(["/music/list"])
  }

/**
   * @property {function} save 
   * 保存方法 
   * @returns 根据  if(data?.createdAt || data?.updatedAt) 判断的值 返回不同的值
   */  
  async save(){
    let objectId = this.musicData.objectId || ""
    console.log(objectId)
    let jsonData:any = {}
    jsonData = this.musicData
    console.log(jsonData)
    // 删除系统保留字段
    delete jsonData.updatedAt
    delete jsonData.createdAt
    delete jsonData.objectIder
    // 发送新增/更新请求   PUT 修改    POST 新增
    let httpMethod = "PUT"
    let url = `http://metapunk.cn:9999/parse/classes/MusicAlbumItem/${objectId}`
    console.log(url)
    if(!this.isUpdate){
      httpMethod = "POST"
      url = `http://metapunk.cn:9999/parse/classes/MusicAlbumItem`
    }
    let response = await fetch(url, {
      "headers": {
        "content-type": "text/plain;charset=UTF-8",
        "x-parse-application-id": "dev"
      },
      // JSON.stringify()将某个对象转换成 JSON 字符串形式
      "body": JSON.stringify(jsonData),
      "method": httpMethod,
      "mode": "cors",
      "credentials": "omit"
    });
    let data = await response.json();
    console.log(data)
    if(data?.createdAt || data?.updatedAt){
      console.log("保存成功")
      this.back();
    }else{
      alert("保存出错，请检查")
    }
  return
}

  /**
   * @property {function} getMusicById 
   * 根据传入的id值 获取 MusicAlbumItem表中的参数
   * @param {string} id  对应歌单的id值
   */
  async getMusicById(id:string){
    let url ="http://metapunk.cn:9999/parse/classes/MusicAlbumItem/"
    let paramsWhere = `${id}?`
    url += paramsWhere
    let paramsInclude = "include=album,music,user";
    url += "&" + paramsInclude
    console.log(url)
    let response = await fetch(url, {
      "headers": {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
        "x-parse-application-id": "dev"
      },
      "referrer": "http://127.0.0.1:4040/",
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": null,
      "method": "GET",
      "mode": "cors",
      "credentials": "omit"
    });
    let data = await response.json();
    if(data?.objectId){
      this.musicData = data
      console.log(this.musicData)
    }
  }
}
